
interface Info {
    token: string,
    userid: string,
}

interface Login {
    loginname:string,
    password:string,
}


import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import {doLoginAPI} from "@/api/user"




export const useUserStore = defineStore('user', () => {

    const userInfo = reactive<Info>({
        token: "",
        userid: "",
    })

    const isLogin = computed(()=>{
        return !!(userInfo.token&&userInfo.userid);
    })

    const updateUserInfo = (data:Info)=>{
        for(const key in data){
            userInfo[key] = data[key];
        }
    }

    const loginAndUpdateUserInfo =(parmas:Login)=>{
        return  doLoginAPI(parmas).then((res:any)=>{
            updateUserInfo(res.data);
        })
    }

    const exitAndUpdateUserInfo =(parmas:Login)=>{
        updateUserInfo({
            token: "",
            userid: "",
        });
    }




    return { userInfo ,isLogin,loginAndUpdateUserInfo,exitAndUpdateUserInfo}
},{
    persist: true,  // 默认在当前模块中开启数据持久化
  })
